//function getUrlParam(name)
//{
//    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
//    var r = window.location.search.substr(1).match(reg);
//    if (r!=null) return unescape(r[2]);
//    return null;
//}

function vensParam(name){
    //去除函数
    String.prototype.trim = function (char, type) {
        if (char) {
            if (type == 'left') {
                return this.replace(new RegExp('^\\'+char+'+', 'g'), '');
            } else if (type == 'right') {
                return this.replace(new RegExp('\\'+char+'+$', 'g'), '');
            }
            return this.replace(new RegExp('^\\'+char+'+|\\'+char+'+$', 'g'), '');
            }
            return this.replace(/^\s+|\s+$/g, '');
        };
			var newArray = new Array();
			var searchUrl =window.location.search;
				searchUrl = searchUrl.trim('?','left');
			if (searchUrl != null){
				var searchData =searchUrl.split("&");//截取 url中的“&”,获得“=”后面的参数
				if(searchData.length > 0){
					$.each(searchData, function(index,key){
						 newArray[key.split("=")[0]] =decodeURI(key.split("=")[1]);
					})
					if( newArray[name] == undefined || newArray[name] == null || newArray[name] == ''){
						return null;
					}else{
						return newArray[name];
					}
				}else{
					return null;
				}
			}else{
				return null;
			}
}


$(function(){
      var type=vensParam('type');
      var key=vensParam('key');
    $.get(
    "Search",
    "type="+type+"&key="+key,
    function(data){
        var table=$("<table class='table_book'></table>");
                var title=$("<tr><th>ID</th><th>图片</th><th>书名</th><th>作者</th><th>梗概</th><th>价格</th><th></th><th></th></tr>");
                table.append(title);
                for(var book of data){
                    var tr=$("<tr></tr>");
                    tr.append("<td>"+book.id+"</td>");
                    tr.append("<td>"+"<img class='book-img' src='./images/upload/"+book.picture+"'>"+"</td>");
                    tr.append("<td>"+book.name+"</td>");
                    tr.append("<td>"+book.author+"</td>");
                    tr.append("<td>"+book.content+"</td>");
                    tr.append("<td>"+book.price+"</td>");
                    tr.append("<td><a href='./DeleteBook?bookId="+book.id+"' style='color:black'>删除</a></td>");
                    tr.append("<td><a href='./update.html?bookId="+book.id+"' style='color:black'>修改</a></td>");
                    table.append(tr);
                }
        $("#searchResult").append(table);
    },
    "json"
    );
});